<?php

namespace app\beautiful\model\finance;

use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\common\model\CommonModel;
use think\facade\Db;


class SettleOrderModel extends CommonModel{

    protected  $name = 'beautiful_settle_order';

    protected  $pk   = 'id';

    public  function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_settle_order')->alias('so')
            ->join('beautiful_order o','o.order_id=so.order_id')
            ->join('beautiful_member m','m.member_id=o.member_id')
            ->join('beautiful_member_level ml','ml.member_id=m.member_id')
            ->leftJoin('beautiful_level l','l.level_id=ml.level_id')
            ->when(isset($where['order_ids']),function ($query) use ($where){
                $query->whereIn('so.order_id',$where['order_ids']);
            })
            ->when(!empty($where['settle_id']),function ($query) use ($where){
                $query->where(['so.settle_id'=>$where['settle_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['so.shop_id'=>$where['shop_id'],'ml.shop_id'=>$where['shop_id']]);
            })
            ->field("so.settle_money,so.order_id,o.total_price,o.need_pay,o.member_coupon_id,o.order_type,o.coupon_money,m.nick_name,m.member_num,m.mobile,m.face,l.name,o.order_sn,o.pay_type");
        $query = $query->order($order);
        $count = $query->count();
        $list = $query->limit($limit)->page($page)
            ->select()
            ->toArray();
        $type_means=[
            'weixin'=>'微信支付',
            'platform_money'=>'钱包支付',
            'money'=>'储值卡支付',
            'offline' => '线下补单',
        ];
        foreach ($list as $k=>$v){
            $list[$k]['settle_money'] = sprintf("%.2f",$v['settle_money']);
            $return = $this->youhui($v);
            $list[$k]['total_num'] = $return['total_num'];
            $list[$k]['goods_name'] = $return['goods_name'];
            $list[$k]['youhui_name'] = $return['youhui_name'];
            $list[$k]['youhui'] = $return['youhui'];
            $list[$k]['pay_type_means'] = $type_means[$v['pay_type']] ?? '-';
        }

        return compact('count','list');
    }
    public function youhui($datas){
        $youhui_name = $goods_name = '';
        $youhui = [];
        $activity = '无';
        //优惠券折扣
        if($datas['member_coupon_id']>0){
            $coupon = MemberCouponModel::with(['couponInfo'])->where(['member_coupon_id'=>$datas['member_coupon_id']])->find();
            if($coupon){
                $coupon = $coupon->toArray();
                $youhui[] = [
                    'discount_name'=>$coupon['couponInfo']['name'],
                    'discount_price'=>$datas['coupon_money']
                ];
                $youhui_name.='/券抵扣';
            }
        }
        //优惠
        if($datas['order_type']=='pin_tuan'){
            $youhui[] = [
                'discount_name'=>'拼团价',
                'discount_price'=>$datas['coupon_money']
            ];
            $youhui_name.='/拼团';
            $activity = '拼团';
        }
        if($datas['order_type']=='flash_sale'){
            $youhui[] = [
                'discount_name'=>'秒杀价',
                'discount_price'=>$datas['coupon_money']
            ];
            $youhui_name.='/秒杀';
            $activity = '秒杀';
        }

        $data = GoodsOrderItemModel::where(['order_id'=>$datas['order_id']])->select();
        //数量
        $total_num = 0;
        foreach ($data as $k=>$v){
            if($k==0){
                $goods_name.=$v['goods_name'];
            }
            if($k==1){
                $goods_name.='...';
            }
            $total_num += $v['goods_num'];
            //会员折扣
            if($v['vip_discount_type']>0){
                if($v['vip_discount_type'] == 1){
                    $discount_name = $v['discount_detail'].'折';
                }else if($v['vip_discount_type'] == 2){
                    $discount_name = '指定价格';
                }else if($v['vip_discount_type'] == 3){
                    $discount_name = '减价';
                }
                if($youhui_name=='' || ($youhui_name!='' && !strpos('会员折扣',$youhui_name))){
                    $youhui_name.='/会员折扣';
                }
                $youhui[] = [
                    'discount_name'=>$discount_name,
                    'discount_price'=>$v['goods_original_price']-$v['goods_price']
                ];
            }
        }

        $return = [
            'goods_name'=>$goods_name,
            'total_num'=>$total_num,
            'youhui_name'=>$youhui_name!=''?substr($youhui_name,1):'',
            'youhui'=>$youhui,
            'activity'=>$activity
        ];

        return $return;
    }
    public  function buyList($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_settle_order')->alias('so')
            ->join('beautiful_buy_order bo','bo.buy_order_id=so.b_order_id')
            ->join('beautiful_member m','m.member_id=bo.member_id')
            ->join('beautiful_member_level ml','ml.member_id=m.member_id')
            ->leftJoin('beautiful_level l','l.level_id=ml.level_id')
            ->when(!empty($where['settle_id']),function ($query) use ($where){
                $query->where(['so.settle_id'=>$where['settle_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['so.shop_id'=>$where['shop_id'],'ml.shop_id'=>$where['shop_id']]);
            })
            ->field("so.id,so.settle_money,so.b_order_id,bo.balance as total_price,bo.discount_detail,bo.member_coupon_id,bo.pay_type,bo.pay_money as need_pay,bo.coupon_money,m.nick_name,m.member_num,m.mobile,m.face,l.name as level_name");
        $query=$query->order($order);
        $count = $query->count();
        $list= $query->limit($limit)->page($page)
            ->select()
            ->toArray();
        $type_means=[
            'weixin'=>'微信支付',
            'platform_money'=>'钱包支付',
            'money'=>'储值卡支付',
            'offline' => '线下补单',
        ];
        foreach ($list as $k=>$v){
            $return = $this->buyYouhui($v);
            $list[$k]['settle_money'] = sprintf("%.2f",$v['settle_money']);
            $list[$k]['youhui_name'] = $return['youhui_name'];
            $list[$k]['youhui'] = $return['youhui'];
            $list[$k]['pay_type_means'] = $type_means[$v['pay_type']] ?? '-';
        }

        return compact('count','list');
    }
    function buyYouhui($datas){
        $youhui_name = '';
        $youhui = [];
        //优惠券折扣
        if($datas['member_coupon_id']>0){
            $coupon = MemberCouponModel::with(['couponInfo'])->where(['member_coupon_id'=>$datas['member_coupon_id']])->find();
            if($coupon){
                $coupon = $coupon->toArray();
                $youhui[] = [
                    'discount_name'=>$coupon['couponInfo']['name'],
                    'discount_price'=>$datas['coupon_money']
                ];
                $youhui_name.='/券抵扣';
            }
        }
        //会员折扣
        if($datas['discount_detail']>0){
            $youhui_name.='/会员折扣';
            $youhui[] = [
                'discount_name'=>$datas['discount_detail'].'折',
                'discount_price'=>$datas['balance']/$datas['discount_detail']-$datas['balance']
            ];
        }
        $return = [
            'youhui_name'=>$youhui_name!=''?substr($youhui_name,1):'',
            'youhui'=>$youhui,
        ];

        return $return;
    }
}